package com.tanhua.server.service;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tanhua.domain.db.Announcement;
import com.tanhua.domain.vo.AnnouncementVo;
import com.tanhua.domain.vo.PageResult;

import com.tanhua.dubbo.api.mongo.AnnouncementApi;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.BeanUtils;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

@Service
public class AnnouncementService {

    @Reference
    private AnnouncementApi announcementApi;

    /**
     *接口名称：公告列表
     * @param page
     * @param pagesize
     */
    public ResponseEntity<Object> findByPage(Integer page, Integer pagesize) {
        //调用接口查询分页数据
        IPage<Announcement> iPage = announcementApi.findByPage(page, pagesize);
        //获取当前页数据
        List<Announcement> announcementList = iPage.getRecords();

        //封装vo对象
        List<AnnouncementVo> result = new ArrayList<>();
        if (announcementList!=null) {
            for (Announcement announcement : announcementList) {
                AnnouncementVo vo = new AnnouncementVo();
                BeanUtils.copyProperties(announcement,vo);
                vo.setId(announcement.getId().toString());
                vo.setCreateDate(new SimpleDateFormat("yyyy-MM-dd HH:mm").format(announcement.getCreated()));
                result.add(vo);
            }
        }
        //构造分页结果返回
        PageResult pageResult = new PageResult(page,pagesize, (int) iPage.getTotal(),result);
        return ResponseEntity.ok(pageResult);
    }
}